<?php
namespace App\Helpers;

/**
 * Created by PhpStorm.
 * User: huyang
 * Date: 2017/8/17
 * Time: 17-10-27
 * Info:xinge helper
 */
use liasica\XingeApp\ClickAction;
use liasica\XingeApp\Message;
use liasica\XingeApp\MessageIOS;
use liasica\XingeApp\Style;
use liasica\XingeApp\TimeInterval;
use liasica\XingeApp\XingeApp;

class XingeHelper
{
    private $andro;
    private $ios;

    public function __construct()
    {
        $this->andro=new XingeAndroHelper;
        $this->ios=new XingeIosHelper;
    }


    /**
     * @param $account string  推送的accessid
     * @param $content string  推送的message
     * @param $custom   array['action','data]   回调转发数据
     * @return bool 推送一端成功返回true
     * @info 推送单一设备
     */
    public function xingeOne($account,$content,$custom){
        $androRe=$this->andro->xingeAndroOne($account,$content,$custom);
        $iosRe=$this->ios->xingeIosOne($account,$content,$custom);
        return $this->resetRespons($androRe,$iosRe);
    }


    /**
     * @param $accountList array  推送的accessid数组
     * @param $content string  推送的message
     * @param $custom array['action','data]   回调转发数据
     * @return bool 推送一端成功返回true
     * @info 推送多设备
     */
    public function xingeList($accountList,$content,$custom){
        $androRe=$this->andro->xingeAndroList($accountList,$content,$custom);
        $iosRe=$this->ios->xingeIosList($accountList,$content,$custom);
        return $this->resetRespons($androRe,$iosRe);
    }

    /**
     *
     */
    /**
     * @param $content  string  推送的信息内容
     * @param $custom   array['action','data]   回调转发数据
     * @return bool 是否推送成功
     * @info    推送所有设备
     */
    public function xingePushAll($content,$custom){
        $androRe=$this->andro->xingePushAndroAll($content,$custom);
        $iosRe=$this->ios->xingePushIosAll($content,$custom);
        return $this->resetRespons($androRe,$iosRe);
    }


    /**
     * @param $andor   安卓推送的结果
     * @param $ios     ios推送的结果
     * @return bool
     */
    private function resetRespons($andor,$ios){
//        如果两个都不为空
        if($andor == false && $ios == false){
            return false;
//            如果有一端推送成功
        }elseif($andor||$ios){
            return true;
        }
        return true;
    }
}

class XingeAndroHelper
{
    private $accessId = '';
    private $accessSecret = '';

    public function __construct()
    {
        $this->accessId = \Config::get('xinge.andoraccessid');
        $this->accessSecret = \Config::get('xinge.andorsecretkey');

    }


    /**
     * @info  信鸽推送单一安卓设备
     * @param $account
     * @param $content
     * @param $custom
     * @return boole
     */
    public function xingeAndroOne($account,$content,$custom){
        $push=new XingeApp($this->accessId,$this->accessSecret);
        $mess = new Message();
        $mess->setType(Message::TYPE_NOTIFICATION);
        $mess->setTitle(\Config::get('xinge.pushtitle'));
        $mess->setContent($content);
        $mess->setExpireTime(86400);
        $style = new Style(0,1,1,0,0);
        $action = new ClickAction();
        $action->setActionType(ClickAction::TYPE_ACTIVITY);
        $action->setComfirmOnUrl(1);
        $mess->setStyle($style);
        $mess->setAction($action);
        $mess->setCustom($custom);
        $acceptTime1 = new TimeInterval(0, 0, 23, 59);
        $mess->addAcceptTime($acceptTime1);
        $ret = $push->PushSingleAccount(0,$account, $mess);
        $ret=$this->resetRespons($ret,$account);
        return $ret;
    }




    /**
     * @info  信鸽推送多安卓设备
     * @param $accountList  array   推送的用户组
     * @param $content  string  推送的消息内容
     * @param $custom   array   参数数组
     * @return boole|array
     * @description 多用户下发有可能有单一设备推送失败也返回true
     */
    public function xingeAndroList($accountList,$content,$custom){
        $push = new XingeApp($this->accessId,$this->accessSecret);

        $mess = new Message();
        $mess->setType(Message::TYPE_NOTIFICATION);
        $mess->setTitle(\Config::get('xinge.pushtitle'));
        $mess->setContent($content);
        $mess->setExpireTime(86400);
        $style = new Style(0,1,1,0,0);
        $action = new ClickAction();
        $action->setActionType(ClickAction::TYPE_ACTIVITY);
        $action->setComfirmOnUrl(1);
        $mess->setStyle($style);
        $mess->setAction($action);
        $mess->setCustom($custom);
        $acceptTime1 = new TimeInterval(0, 0, 23, 59);
        $mess->addAcceptTime($acceptTime1);
        $ret = $push->PushAccountList(0, $accountList, $mess);
        $ret=$this->resetRespons($ret);
        return $ret;
    }


    /**
     * @param $content  string  推送的消息内容
     * @param $custom   array   参数数组
     * @return bool
     * @info    推送安卓所有设备
     */
    public function xingePushAndroAll($content,$custom)
    {
        $push = new XingeApp($this->accessId,$this->accessSecret);
        $mess = new Message();
        $mess->setType(Message::TYPE_NOTIFICATION);
        $mess->setTitle(\Config::get('xinge.pushtitle'));
        $mess->setContent($content);
        $mess->setExpireTime(86400);
        $mess->setCustom($custom);
        #含义：样式编号0，响铃，震动，不可从通知栏清除，不影响先前通知
        $style = new Style(0,1,1,0,0);
        $action = new ClickAction();
        $action->setActionType(ClickAction::TYPE_ACTIVITY);
        #打开url需要用户确认
        $action->setComfirmOnUrl(1);
        $mess->setStyle($style);
        $mess->setAction($action);
        $ret = $push->PushAllDevices(0, $mess);
        $ret=$this->resetRespons($ret);
        return ($ret);
    }


    /**
     * @param $data
     * @return bool
     * @info 重组返回需要的respons信息
     */
    private function resetRespons($data,$account=""){
        if(!isset($data['ret_code'])){
            $data['ret_code']=-115;
            $data['err_msg']='未知错误，请重试后联系管理员';
        }
        if($data['ret_code']===0){
            return true;
        }
        logStore('warning',$account.'用户apppush推送失败:'.$data['ret_code'].'-'.$data['err_msg']);
        return $data;
    }

}


class XingeIosHelper
{
    private $accessId = '';
    private $accessSecret = '';

    public function __construct()
    {
        $this->accessId = \Config::get('xinge.iosaccessid');
        $this->accessSecret = \Config::get('xinge.iossecretkey');

    }


    /**
     * @info  信鸽推送单一ios设备
     * @param $account
     * @param $content
     * @param $custom
     * @return array
     */

    public function xingeIosOne($account,$content,$custom){
//        echo 'xingeIosOne';die;
        $push = new XingeApp($this->accessId,$this->accessSecret);
//        XingeApp::PushAccountIos();
        $mess = new MessageIOS();
        $mess->setExpireTime(86400);
        $alertData=array(
            'title'=>\Config::get('xinge.pushtitle'),
            'body'=>$content,
        );
        $mess->setAlert($alertData);
        $mess->setBadge(1);
        $mess->setSound("beep.wav");
        $mess->setCustom($custom);
        $acceptTime1 = new TimeInterval(0, 0, 23, 59);
        $mess->addAcceptTime($acceptTime1);

        $ret = $push->PushSingleAccount(0, $account, $mess, \Config::get('xinge.iosenv'));
        $ret=$this->resetRespons($ret,$account);
        return $ret;
//        --------------------
    }



    /**
     * @info ios下发多设备
     * @param $accountList  array   推送的用户组
     * @param $content  string  推送的消息内容
     * @param $custom   array   参数数组
     */
    public function xingeIosList($account,$content,$custom){
        $push = new XingeApp($this->accessId,$this->accessSecret);
        $mess = new MessageIOS();
        $mess->setExpireTime(86400);
        $alertData=array(
            'title'=>\Config::get('xinge.pushtitle'),
            'body'=>$content,
        );
        $mess->setAlert($alertData);
        $mess->setBadge(1);
        $mess->setSound("beep.wav");
        $mess->setCustom($custom);
        $acceptTime1 = new TimeInterval(0, 0, 23, 59);
        $mess->addAcceptTime($acceptTime1);
        $ret = $push->PushAccountList(0, $account, $mess, \Config::get('xinge.iosenv'));
        $ret=$this->resetRespons($ret);
        return $ret;
    }

    /**
     * @param $content
     * @param $custom
     * @return array
     * @info  推送ios所有设备
     */
    public function xingePushIosAll($content,$custom)
    {
        $push = new XingeApp($this->accessId,$this->accessSecret);
        $mess = new MessageIOS();
        $mess->setExpireTime(86400);
        $alertData=array(
            'title'=>\Config::get('xinge.pushtitle'),
            'body'=>$content,
        );
        $mess->setAlert($alertData);
        $mess->setBadge(1);
        $mess->setSound("beep.wav");
        $mess->setCustom($custom);
        $acceptTime1 = new TimeInterval(0, 0, 23, 59);
        $mess->addAcceptTime($acceptTime1);
        $ret = $push->PushAllDevices(0, $mess,\Config::get('xinge.iosenv'));
        $ret=$this->resetRespons($ret);
        return $ret;
    }

    /**
     * @param $data 结果集数据
     * @return bool  是否推送成功
     */
    private function resetRespons($data,$account=''){
        if(!isset($data['ret_code'])){
            $data['ret_code']=-115;
            $data['err_msg']='未知错误，请重试后联系管理员';
        }
        if($data['ret_code']===0){
            return true;
        }
        logStore('warning',$account.'用户apppush推送失败:'.$data['ret_code'].'-'.$data['err_msg']);
        return $data;
    }

}
